package com.syh.jn.scm.domain.operation.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.core.ddd.domain.CodeExpEnum;
import com.syh.core.ddd.domain.CodeGenerateUtil;
import com.syh.jn.scm.domain.operation.repository.system.OperationAccountReadRepository;
import com.syh.jn.scm.dto.operation.system.account.OperationAccountMto;
import com.syh.jn.scm.dto.operation.system.account.OperationAccountSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.UUID;

/**
 * 运营账号管理
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class OperationAccount extends AbstractSimpleAggregate<Long> {
  /**
   * 登录账号
   */
  private String loginAccount;
  /**
   * 登录密码
   */
  private String loginPassword;
  /**
   * 是否超管 1 是 -1 否
   */
  private Short admin;
  /**
   * 数据归属公司ID
   */
  private Long businessEntityId;
  /**
   * 姓名
   */
  private String name;
  /**
   * 电话
   */
  private String telephone;
  /**
   * 角色id
   */
  private Long roleId;
  /**
   * 使用情况:-1冻结, 1启用
   */
  private Short enable;

  public OperationAccount(OperationAccountSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    OperationAccountReadRepository readRepository = (OperationAccountReadRepository) SpringContextUtils.getBean(OperationAccountReadRepository.class);
    //生成带code账号
    StringBuffer operaAccount = new StringBuffer(sto.getLoginAccount())
            .append("@").append(CodeGenerateUtil.operaCodeGenerate(CodeExpEnum.OPERATION_CODE_EXP.getCodeExp()));
    sto.setLoginAccount(operaAccount.toString());
    //查看账号是否已经存在
    Long count = readRepository.getCountByAccountName(sto.getLoginAccount());
    if (count.intValue() > 0) {
      throw new BusinessException("帐号已经存在,请更换帐号名重试");
    }
    this.uuid = UUID.randomUUID().toString();
    this.admin = -1;
    this.createTimeStamp = System.currentTimeMillis();
    this.enable = Short.valueOf("1");
    ExtBeanUtils.copyProperties(sto, this, false);
  }

  public static OperationAccount createdSuperAdminOnce() {
    OperationAccountReadRepository operationAccountReadRepository = (OperationAccountReadRepository) SpringContextUtils.getBean(OperationAccountReadRepository.class);
    List<OperationAccount> operationAccountList = operationAccountReadRepository.getList(Wrapper.build().append(Restrictions.eq("loginAccount", "system@syhvip")));
    if (!operationAccountList.isEmpty()) {
      return operationAccountList.get(0);
    }
    OperationAccount superAdmin = new OperationAccount();
    superAdmin.setUuid(UUID.randomUUID().toString());
    superAdmin.setAdmin(Short.valueOf("1"));
    superAdmin.setRoleId(0l);
    superAdmin.setCreateTimeStamp(System.currentTimeMillis());
    superAdmin.setEnable(Short.valueOf("1"));
    superAdmin.setLoginAccount("system@syhvip");
    superAdmin.setLoginPassword(PasswordUtils.generate("a12345678"));
    superAdmin.setName("运营管理员");
    superAdmin.setBusinessEntityId(0l);
    return superAdmin;
  }

  public void modify(OperationAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    OperationAccountReadRepository readRepository = (OperationAccountReadRepository) SpringContextUtils.getBean(OperationAccountReadRepository.class);
    //重置密码时获取数据库的账号需要预处理
    String suffix = "@" + CodeExpEnum.OPERATION_CODE_EXP.getCodeExp();
    if (mto.getLoginAccount().indexOf(suffix) > 0) {
      mto.setLoginAccount(mto.getLoginAccount().split(suffix)[0]);
    }
    //生成带code账号
    StringBuffer operaAccount = new StringBuffer(mto.getLoginAccount()).append(suffix);
    mto.setLoginAccount(operaAccount.toString());
    //查看账号是否已经存在
    Long count = readRepository.getCountByAccountName(mto.getLoginAccount(), mto.getId());
    if (count.intValue() > 0) {
      throw new BusinessException("帐号已经存在,请更换帐号名重试");
    }

    if (this.enable.equals(Short.valueOf("-1"))) {//冻结将所属角色取消
      this.roleId = null;
    }
    this.lastUpdateTimeStamp = System.currentTimeMillis();
    ExtBeanUtils.copyProperties(mto, this, false);
  }

  public void changeEnable(Short enable) {
    this.enable = enable;
    /*if (this.enable.equals(Short.valueOf("-1"))) {//冻结将所属角色取消
      this.roleId = null;
    }
    this.lastUpdateTimeStamp = System.currentTimeMillis();*/
  }

  public void changeRole(Long roleId) {
    ExtAssert.notNull(roleId, "id为空");
    this.roleId = roleId;
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  public void destroy() {
    if (this.enable.equals(Short.valueOf("1"))) {
      throw new BusinessException("此账号正在使用中,不能删除");
    }
  }
}
